package csel.model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class GameFactory implements java.io.Serializable
{
	private static final long serialVersionUID = 1L;

	public static boolean newGame(Occupation occupation, String name, Model model)
	{
		Map myMap;
		try
		{
			myMap = new GameFactory().loadMap("defaultMap");
		}
		catch(FileNotFoundException e)
		{
			System.out.println(e.toString());
			return false;
		}
		catch(IOException e)
		{
			System.out.println(e.toString());
			return false;
		}
		Entity player = new Entity(occupation);
		player.setName(name);
		System.out.print("Player: " + player.getName() + " has the following stats: ");
		System.out.println("Strength: " + player.getStrength() + ", Agility: "+ player.getAgility() + ", Intellect: " + player.getIntellect() + "Hardiness: " + player.getHardiness());
		
		Tile myTile = myMap.getTile(5, 4);
		myTile.setEntity(player);
		player.setLocation(myTile);
		
		ArrayList<Entity> entityList = new ArrayList<Entity>();
		entityList.add(player);
		GameData gameData = new GameData(entityList, player, myMap);
		model.set_gameData(gameData);
	
		
		
		System.out.println(myTile);
		System.out.println(player.getLocation());
		System.out.println(gameData.get_playerEntity());
		
		return true;
	}
	private Map loadMap(String filepath) throws FileNotFoundException,
	IOException
	{
		FileInputStream fileIn;
		ObjectInputStream serializer;
		Map map;

		fileIn = new FileInputStream(filepath);
		serializer = new ObjectInputStream(fileIn);

		try
		{
			map = (Map) serializer.readObject();
		} catch (ClassNotFoundException ex)
		{
			throw new IOException("Invalid save file data.", ex);
		}

		serializer.close();
		fileIn.close();

		return map;
	}
}
